package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.*;
import com.rico.deploy.service.ClazzService;
import com.rico.deploy.service.SubjectService;
import com.rico.deploy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 夏智力
 * @FileName ClazzController
 * @date 2021/5/16 10:26
 * @Software: IntelliJ IDEA
 */

@Slf4j
@Controller
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private UserService userService;

    @GetMapping("clazz/lists")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit, @RequestParam(value="keyword",defaultValue = "计算机科学与技术") String keyword) {
        log.info("{} {} {}", page, limit, keyword );
        List<Clazz> clazzes = clazzService.selectAll(page, limit ,keyword);
        PageInfo<Clazz> clazzPageInfo = new PageInfo(clazzes);
        return MessageUtils.success(clazzPageInfo.getList(), (int) clazzPageInfo.getTotal());
    }


    @GetMapping("clazz/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( clazzService.deleteClazzById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }

    /**
     * 更新数据
     * @param clazz
     * @return
     */
    @PostMapping("clazz/put")
    @ResponseBody
    public RestResponse put(Clazz clazz) {
        clazz.setUserName(userService.selectUserById(clazz.getUserId()).getName());
        clazz.setModifyTime( new Date(new Date().getTime()+28800000));
        int i = clazzService.updateClazz(clazz);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    /**
     * 添加数据
     * @param clazz
     * @return
     */

    @PostMapping("clazz/post")
    @ResponseBody
    public RestResponse post(Clazz clazz) {

        int i = clazzService.insertClazz(clazz);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 添加页面
     * @param clazz
     * @return
     */
    @GetMapping("clazz/add")
    public String htmladd(Model model){
        return "page/manage/clazzManage/add";
    }

    /**
     * 学院左边树
     * @return
     */
    @GetMapping("clazz/leftMenu")
    @ResponseBody
    public RestResponse LeftTreeView(){
        return MessageUtils.success("操作成功",subjectService.LeftTreeView());
    }

    @GetMapping("clazz/edit")
    public String htmledit(Integer id, Model model){
        log.info(id.toString());
        Clazz clazz = clazzService.selectById(id);
        List<User> users = userService.selectRoles(3);
        model.addAttribute("clazz",clazz);
        model.addAttribute("users",users);
        return "page/manage/clazzManage/edit";
    }
    /**
     * 获取html页面
     * @return
     */
    @GetMapping("clazzManage")
    public String gethtml(){
        return "page/manage/clazzManage/clazzManage";
    }
}
